# !/usr/bin python3
# encoding: utf-8 -*-
# @file     : json_util.py
# @author   : 沙陌 Matongxue_2
# @Time     : 2024/1/6 15:02
# @Copyright: 北京码同学
import jsonpath
from jsonpath_ng import parse, Index, Fields

from common.logger import GetLogger


def update_value_to_json(json_object,json_path,new_value):
    """

    :param json_object:
    :param json_path:
    :param new_value: 目标参数对应的测试数据，如果new_value得值是$del则代表你要删除目标参数
    :return:
    """
    jsonpath_expr = parse(json_path) # 把传进来的json_path表达式转成jsonpath对象
    matches = jsonpath_expr.find(json_object) # 通过jsonpath匹配目标数据
    for match in matches:
        path = match.path # 获取当前匹配对象的路径
        if isinstance(path,Index): # 判断他是不是一个索引对象路径
            if new_value == '$del':
                del match.context.value[match.path.index]
            else:
                match.context.value[match.path.index] = new_value
        elif isinstance(path,Fields): # 判断他是不是一个字段对象
            if new_value == '$del':
                del match.context.value[match.path.fields[0]]
            else:
                match.context.value[match.path.fields[0]] = new_value
    return json_object

def extract_json(json_object,json_path,index=0):
    """

    :param json_object:
    :param json_path:
    :param index: 默认是0，表示默认会得到提到的第一个数据，如果你想拿到匹配到的所有之，那么传-1，如果你想拿到结果中的第几个就传数字索引
    :return:
    """
    logger = GetLogger.get_logger()
    res = jsonpath.jsonpath(json_object,json_path)
    # res如果提取到了值那么他就是一个列表，如果没匹配到他是False
    if res:
        logger.info(f'通过{json_path}匹配到的结果是:{res}')
        if index<0:
            # 如果index小于0，则认为你想要所有的匹配结果
            return res
        else:
            # 如果不小于0，那么你传几，就代表你要的是匹配到的某一个
            return res[index]
    else:
        logger.exception(f'通过{json_path}没有提取到值')
if __name__ == '__main__':
    aaa = {
            "brand_id": "",
            "category_id": 83,
            "category_name": "",
            "goods_name": "沙陌炒锅-20231114班",
            "sn": "sn001",
            "price": "199",
            "mktprice": "189",
            "cost": "30",
            "weight": "1",
            "goods_gallery_list": [{
                "img_id": -1,
                "original": "http://59.36.173.55:7000/statics/attachment/goods/2024/1/4/21/00544529.png",
                "sort": 0
            }],
            "quantity": 99999999,
            "goods_transfee_charge": 1,
            "has_changed": 0,
            "market_enable": 1,
            "template_id": 0,
            "exchange": {
                "category_id": "",
                "enable_exchange": 0,
                "exchange_money": 0,
                "exchange_point": 0
            },
            "shop_cat_id": 0,
            "meta_description": "",
            "meta_keywords": "",
            "page_title": "",
            "goods_params_list": [],
            "sku_list": [],
            "intro": "<p>商品说明</p>"
        }

    # print(update_value_to_json(json_object=aaa, json_path='$.goods_name', new_value=''))
    # print(update_value_to_json(json_object=aaa, json_path='$.exchange.enable_exchange', new_value=-1))

    print(update_value_to_json(json_object=aaa, json_path='$.goods_name', new_value='$del'))
